﻿layui.use(['form', 'table', 'http', 'popup', 'upload'], function () {
    var form = layui.form;
    var table = layui.table;
    var $ = layui.$;
    var popup = layui.popup;
    var http = layui.http;
    var upload = layui.upload;
    table.render({
        elem: '#dataTable',
        url: '/Book/GetList',
        where: { LibraryId: $("#LibraryId").val() },
        height: 'full',
        page: true,
        skin: 'line',
        toolbar: '#toolbar',
        defaultToolbar: [{
            title: '刷新',
            layEvent: 'refresh',
            icon: 'layui-icon-refresh',
        }, 'filter', 'print', 'exports'],
        cols: [[
            { field: 'name', title: '书名' },
            { field: 'author', title: '作者' },
            { field: 'ISBN', title: 'ISBN' },
            { field: 'press', title: '出版社' },
            { field: 'libraryName', title: '图书室' },
            { field: 'bookshelfCode', title: '书架' },
            { field: 'createdTime', title: '创建时间' },
            { title: '操作', toolbar: '#rowbar' },
        ]],
        done: function (res, curr, count) {
            if (res.code === 1) {
                popup.toastError(res.msg);
            }
        }
    });

    // 表格上方的按钮
    table.on('toolbar(dataTable)', function (obj) {
        if (obj.event === 'add') {
            add();
        } else if (obj.event === 'refresh') {
            refresh();
        }
    });
    // 行 操作
    table.on('tool(dataTable)', function (obj) {
        if (obj.event === 'edit') {
            add(obj.data);
        } else if (obj.event === 'remove') {
            layer.confirm(`确定删除${obj.data.code}?`, { icon: 3, title: '提示' }, function (index) {
                var loading = layer.load(2);
                http.ajax({
                    url: '/Book/Delete',
                    data: { Id: obj.data.id },
                    success: function (res) {
                        layer.close(loading);
                        if (res.code === 0) {
                            popup.toastSuccess(res.msg);
                            layer.close(index);
                            refresh();
                        } else {
                            popup.toastError(res.msg);
                        }
                    },
                    error: function () {
                        layer.close(loading);
                        popup.toastError("请求失败，请稍后重试");
                    }
                });

            });
        }
    });

    /**
     * 新增
     */
    function add() {
        top.layui.tab.addTabOnlyByElem("content", { id: "", title: "新增图书", url: "/Book/Add", close: "允许关闭" })
    }
    $("#search").click(function () {
        refresh();
    });
    /**刷新 */
    function refresh() {
        var data = { LibraryId: $("#LibraryId").val(), shelfName: $("#shelfName").val() };

        table.reload('dataTable', {
            where: data,
            page: {
                curr: 1
            }
        });
    }

    form.on('select(LibraryId)', function (data) {
        http.ajax({
            url: '/Book/GetBookshelf',
            type: 'get',
            data: { libraryId: data.value},
            success: function (res) {
                if (res) {
                    var html = '';
                    $.each(res, function (i, item) {
                        html += ' <option value="' + item.value + '">' + item.text + '</option>';
                    })
                    $("#bookselfId").html(html);
                    form.render('select');
                }
            }
        });
    });

    upload.render({
        elem: '#uploadCover',
        url: '',
        done: function (res) {
           
        },
        error: function () {
            //请求异常回调
        }
    });


});